package com.zwy.fornote.controller;

import com.github.pagehelper.PageInfo;
import com.zwy.fornote.entity.req.ArticleReq;
import com.zwy.fornote.entity.req.NewArticleReq;
import com.zwy.fornote.entity.req.UserArticleReq;
import com.zwy.fornote.entity.vo.ArticleManageVO;
import com.zwy.fornote.model.Article;
import com.zwy.fornote.service.ArticleService;
import com.zwy.fornote.util.AjaxResult;
import com.zwy.fornote.util.CurrentUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

    @Resource
    ArticleService articleService;

    @PostMapping("/selectArticle")
    public AjaxResult selectArticle(@RequestBody ArticleReq req)
    {
        try {
            PageInfo<Article> pageInfo = articleService.selectArticle(req);
            return AjaxResult.ok(pageInfo);
        }catch (Exception e)
        {
            return new AjaxResult(-1,"查找文章失败",e.getMessage());
        }

    }

    @PostMapping("/manageArticle")
    public AjaxResult manageArticle(@RequestBody ArticleReq req)
    {
        try {
            PageInfo<ArticleManageVO> pageInfo = articleService.getManageArticleList(req);
            return AjaxResult.ok(pageInfo);
        }catch (Exception e)
        {
            return new AjaxResult(-1,"查找文章失败",e.getMessage());
        }

    }

    @PostMapping("/getArticlesByUser")
    public AjaxResult getArticlesByUser(@RequestBody UserArticleReq req)
    {
        try {
            req.setUserId(CurrentUser.getUser().getId());
            PageInfo<Article> pageInfo = articleService.getArticlesByUser(req);
            return AjaxResult.ok(pageInfo);
        }catch (Exception e)
        {
            return new AjaxResult(-1,"查找用户文章失败",e.getMessage());
        }

    }

    @PostMapping("/getArticlesByUserCollect")
    public AjaxResult getArticlesByUserCollect(@RequestBody UserArticleReq req)
    {
        try {
            req.setUserId(CurrentUser.getUser().getId());
            PageInfo<Article> pageInfo = articleService.getArticlesByUserCollect(req);
            return AjaxResult.ok(pageInfo);
        }catch (Exception e)
        {
            return new AjaxResult(-1,"查找用户收藏文章失败",e.getMessage());
        }

    }

    @PostMapping("/createNewArticle")
    public AjaxResult createNewArticle(@RequestBody NewArticleReq req)
    {
        try {
            int newArticle = articleService.createNewArticle(req);
            if(newArticle>0)
            {
                return AjaxResult.ok("新增文章成功");
            }else {
                return AjaxResult.fail(-2,"新增文章失败");
            }

        }catch (Exception e)
        {
            return new AjaxResult(-1,"查找用户文章失败",e.getMessage());
        }

    }

    @GetMapping("/collectArticle/{artId}")
    public AjaxResult createNewArticle(@PathVariable("artId") int artId)
    {
        try {
            int i = articleService.collectArticle(artId, CurrentUser.getUser().getId());
            if(i>0)
            {
                return AjaxResult.ok("收藏文章成功");
            }
            else if(i==-1)
            {
                return new AjaxResult(33,"已收藏","已收藏");
            }else {
                return AjaxResult.fail(-2,"收藏文章失败");
            }

        }catch (Exception e)
        {
            return new AjaxResult(-1,"收藏文章失败",e.getMessage());
        }

    }
}
